package iotwisp.school.user.entity;

import java.util.Set;

import javax.persistence.*;

import iotwisp.school.user.command.admin.ChangeAdminEnableSCmd;
import iotwisp.school.user.command.admin.CreateAdminSCmd;
import iotwisp.school.user.command.admin.ModifyAdminSCmd;
import iotwisp.school.system.entity.Image;
import iotwisp.base.orm.StringIdBaseEntity;
import iotwisp.base.util.PwdUtil;
import iotwisp.base.util.UUIDGenerator;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.Type;

import iotwisp.school.user.qo.AdminQO;

/**
 * {@link AdminQO} {@link diance.view.organization.vo.AdminVO}
 * {@link diance.view.organization.provider.qo.AdminViewQO}
 * {@link jzjd.organization.service.AdminService} {@link UserSPI} User UserQO
 * UserVO UserSPI UserViewSPI UserSaleStatisticsService UserViewService UserController
 * UserViewController
 *
 * @author yuxx
 */
@Data
@EqualsAndHashCode(callSuper = false)
@DynamicUpdate
@Entity
@Table(name = M.TABLE_PREFIX + "admin")
@SuppressWarnings("serial")
public class Admin extends StringIdBaseEntity {

	/**
	 * 名字
	 */
	@Column(name = "name")
	private String name;

	/**
	 * 电话
	 */
	@Column(name = "phone")
	private String phone;

	/**
	 * 登录名
	 */
	@Column(name = "login_name")
	private String loginName;

	/**
	 * 登录密码
	 */
	@Column(name = "password")
	private String password;

	/**
	 * 帐号是否启用
	 */
	@Type(type = "yes_no")
	@Column(name = "enable")
	private Boolean enable;

	/**
	 * 头像照片
	 */
//	@Column(name = "head_image_id", length = 32)
//	private String headImageId;
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "head_image_id", foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT))
	private Image headImage;

	/**
	 * 拥有的角色集
	 */
	@ManyToMany(fetch = FetchType.LAZY)
	@Cascade({ org.hibernate.annotations.CascadeType.SAVE_UPDATE })
	@JoinTable(name = "org_admin_role_link", joinColumns = { @JoinColumn(name = "admin_id") }, inverseJoinColumns = {
			@JoinColumn(name = "role_id") })
	private Set<Role> roleSet;

	public void create(CreateAdminSCmd cmd, Set<Role> roleSet, Image image) {

		setId(UUIDGenerator.getUUID());
		setName(cmd.getName());
		setPhone(cmd.getPhone());
		setLoginName(cmd.getLoginName());
		setPassword(PwdUtil.getPwd(cmd.getPassword()));
		setEnable(cmd.getEnable());
//		if (!StringUtils.isBlank(cmd.getHeadImageId())) {
//			setHeadImageId(cmd.getHeadImageId());
//		}
		setHeadImage(image);
		setRoleSet(roleSet);
	}

	public void modify(ModifyAdminSCmd cmd, Image image) {

		setName(cmd.getName());
		setPhone(cmd.getPhone());
		setLoginName(cmd.getLoginName());
		if (StringUtils.isNotBlank(cmd.getPassword())) {
			setPassword(PwdUtil.getPwd(cmd.getPassword()));
		}
		setEnable(cmd.getEnable());
		setHeadImage(image);
	}

	public void changeEnable(ChangeAdminEnableSCmd cmd) {
		if ((cmd.getEnable() == null)) {
			setEnable(!getEnable());
		} else {
			setEnable(cmd.getEnable());
		}
	}

}
